package com.module.content.helper

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.allen.app.data.event.InfoGifPlayEvent
import com.allen.base.base.basic.extras.getUrl
import com.hwangjr.rxbus.RxBus
import com.module.content.R
import com.module.content.controller.GSYVideoGLViewCustomRender4
import com.module.content.controller.InfoControlVideo
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.render.effect.GaussianBlurEffect
import com.shuyu.gsyvideoplayer.render.view.GSYVideoGLView
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView

/**
 * author : Allen
 * date   : 2020/08/31
 * time   : 14:09
 * desc   : 列表视频播放工具类
 */
class InfoVideoPlayHelper(
    private val context: Context,
    private val isGif: Boolean = false
) {
    private var playerViewLayout: View? = null
    private var viewVideoPlayer: InfoControlVideo? = null
    private var lastFrameVideo: FrameLayout? = null
    private var curPosition = -1

    fun setVideoPlayer(
        contentLayout: FrameLayout?,
        mediaPath: String?,
        position: Int,
        play: Boolean = false
    ) {
        contentLayout?.bringToFront()
        if (curPosition == position && viewVideoPlayer != null) {
            if (viewVideoPlayer?.currentState == GSYVideoView.CURRENT_STATE_PAUSE) {
                viewVideoPlayer?.onVideoResume()
                return
            }
            if (viewVideoPlayer?.isInPlayingState == true) {
                return
            }
            if (viewVideoPlayer?.currentState == GSYVideoView.CURRENT_STATE_AUTO_COMPLETE && !play) {
                return
            }
        }
        removePlayer()
        val path = mediaPath?.getUrl()
        if (path.isNullOrEmpty()) {
            return
        }
        initPlayer(contentLayout)
        viewVideoPlayer?.playPosition = position
        viewVideoPlayer?.setUp(path, true, "")
        viewVideoPlayer?.startPlayLogic()
        curPosition = position
    }

    fun stop() {
        if (viewVideoPlayer?.isInPlayingState == true) {
            viewVideoPlayer?.onVideoPause()
        }
    }


    @SuppressLint("InflateParams")
    fun initPlayer(contentLayout: FrameLayout?) {
        val layoutInflater = LayoutInflater.from(context)
        playerViewLayout = layoutInflater.inflate(R.layout.info_player_view, null)
        viewVideoPlayer = playerViewLayout?.findViewById(R.id.mPlayerView)
        viewVideoPlayer?.setHideProgress(isGif)
        viewVideoPlayer?.effectFilter = GaussianBlurEffect(6.0f, GaussianBlurEffect.TYPEXY)
        viewVideoPlayer?.setCustomGLRenderer(GSYVideoGLViewCustomRender4())
        viewVideoPlayer?.setGLRenderMode(GSYVideoGLView.MODE_RENDER_SIZE)
        viewVideoPlayer?.setVideoAllCallBack(object : GSYSampleCallBack() {
            override fun onAutoComplete(url: String?, vararg objects: Any?) {
                if (isGif) {
                    removePlayer()
                    RxBus.get().post(InfoGifPlayEvent(curPosition))
                }
            }
        })
        contentLayout?.addView(playerViewLayout)
        lastFrameVideo = contentLayout
    }

    private fun removePlayer() {
        if (null == lastFrameVideo || null == playerViewLayout)
            return

        viewVideoPlayer?.release()
        lastFrameVideo?.removeView(playerViewLayout)
    }

    fun onChildViewDetachedFromWindow(view: View) {
        if (isFullScreen())
            return
        val layout = view.findViewById<FrameLayout>(R.id.contentLayout)
        if (layout != null && layout.childCount > 4) {
            removePlayer()
        }
    }

    private fun isFullScreen(): Boolean {
        return viewVideoPlayer?.isIfCurrentIsFullscreen ?: false
    }
}